package com.hr.剑指offer.work1;

import java.util.Stack;

/**
 * @author 周恒哲
 * @date 2020/06/17
 **/
public class 面试题31_栈的压入和弹出序列 {
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack=new Stack<>();
        int i=0;
        for (int num : pushed) {
            stack.push(num);//入栈
            while (!stack.isEmpty()&&stack.peek()==popped[i]){//循环判断与出栈
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        int[] pushed={1,2,3,4,5};
        int[] popped={4,5,3,2,1};
        System.out.println(面试题31_栈的压入和弹出序列.validateStackSequences(pushed, popped));
    }
}
